package corewarsxxi.logic;

import corewarsxxi.logic.throwables.processJump;
import java.io.Serializable;

/**
 * Instruction <code>jmn</code> from Core Wars.
 * <p>Contains a static self to be used by all instances of MemoryCell.</p>
 * @version ICWS 88
 * @see     corewarsxxi.logic.OpCode
 * @author  Luís Fonseca, ei10139 @ FEUP
 */
public class jmn extends OpCode implements Serializable
{
    /** Simplefied Singleton that provides access to a final static <code>jmn</code> instance.*/
    public static final jmn self = new jmn();

    /**
     * Private constructor of jmn.
     */
    private jmn() {}

    public void operation(MemoryField aField, MemoryField bField) throws processJump
    {
        if(aField.aMode instanceof AMImmediate)
        {
            if(bField.getValue() != 0)
                throw new processJump(0);
        }

        if(bField.aMode instanceof AMImmediate)
        {
            if(bField.getValue() != 0)
                throw new processJump(aField.getValue());

            return;
        }
        if(0 != Core.getInstance().memArray.get(bField.getValue()).bField.getStoredValue())
            throw new processJump(aField.getValue(), false);
    }

    /**
     * Makes use of the Singleton pattern and returns the public static object.
     * @return it<i>self</i>.
     */
    @Override
    public OpCode getSelf() {
        return jmn.self;
    }

    /**
     * Overrides the default Object.toString() function.
     * @return Always returns "<code>jmn</code>".
     */
    @Override
    public String toString()
    {
        return "jmn";
    }
}
